
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.Serializable;
import java.net.Socket;

public class Request implements Serializable {

    private String op;
    private HashMap<Integer, byte[] > data;
    private Address addr;

    public Request(int key, byte[] arrayb, String operation) {
        if (operation.toLowerCase().equals("get")) {
            this.op = "get";
        } else {
            this.op = "put";
        }

        this.data = new HashMap<Integer, byte[]>();
        data.put(key, arrayb.clone());
    }

    public Request(int[] keys, byte[][] arraysb, String operation) {
        if (operation.toLowerCase().equals("get")) {
            this.op = "get";
        } else {
            this.op = "put";
        }

        this.data = new HashMap<Integer, byte[]>();
        for (int i = 0; i < keys.length; i++) {
            data.put(keys[i], arraysb[i].clone());
        }

    }

    public Request(HashMap<Integer,byte[]> dados,Address addr,String op){
        this.op= op;
        this.data = dados;
        this.addr=addr;
    }

    public Request(String op, HashMap<Integer,byte[]> data,Address addr){
        this.op = op;
        this.data = data;
        this.addr = addr;
    }

    public Request() {
        this.op = "rep";
        this.data = new HashMap<Integer, byte[]> ();
        this.addr = new Address(0, op);
    }

    public HashMap<Integer, byte[]> getData() {
        return data;
    }

    public void setData(HashMap<Integer, byte[]> data) {
        this.data = data;
    }

    public String getOp() {
        return op;
    }

    public void setOp(String op) {
        this.op = op;
    }

    public Address getAddress() {
        return this.addr;
    }

    public void setSock(Address addr) {
        this.addr = addr;
    }

    public String toString(){
        StringBuilder s = new StringBuilder();
        s.append("Operacao: "+op);
        for(Integer i: data.keySet()){
            s.append("Chave: "+i+ " valor ="+data.get(i).toString());
        }
        return s.toString();
    }

    public static Byte[] convertbtoB(byte[] array) {
        Byte[] res = new Byte[array.length];
        for (int i = 0; i < array.length; i++) {
            res[i] = new Byte(array[i]);
        }
        return res;
    }

    public static byte[] convertBtob(Byte[] array) {
        byte[] res = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            res[i] = array[i];
        }
        return res;
    }

    public Request clone(){
        return new Request(this.op,this.data,this.addr.clone());    
    }
}
